package com.xianyun.site.iblog.blog.executor.command;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.dto.Response;
import com.xianyun.common.core.utils.bean.BeanUtils;
import com.xianyun.site.iblog.domain.blog.gateway.ArticleGateWay;
import com.xianyun.site.iblog.domain.blog.model.Article;
import com.xianyun.site.iblog.dto.blog.ArticleAddCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.Valid;

/**
 * 文章添加
 * @author mac_zyj
 */
@Slf4j
@Service
@CatchAndLog
public class ArticleAddCmdExo {

    @Autowired
    private ArticleGateWay articleGateWay;

    @Transactional(rollbackFor = RuntimeException.class)
    public Response execute(@Valid  ArticleAddCmd articleAddCmd){
        Article article = new Article();
        BeanUtils.copyProperties(articleAddCmd,article);
       if (null != articleAddCmd.getId()){
           articleGateWay.updateArticle(article);
       } else {
           articleGateWay.addArticle(article);
       }
        return Response.buildSuccess();
    }
}
